﻿using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Data;
using System.Linq;
using dIRca.Data;
using dIRca.WP7.Model;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using GalaSoft.MvvmLight.Threading;
using WP7Contrib.Collections;
using WP7Contrib.Services.Storage;

namespace dIRca.WP7.ViewModels
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// You can also use Blend to data bind with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// </summary>
    public class ChannelViewModel : ViewModelBaseWp7
    {
        #region Commands

        #endregion

        #region Properties
        public IMessagesView Model { get; private set; }
        public CollectionViewSource Messages { get; set; }

        private Message _selectedMessage;
        public Message SelectedMessage
        {
            get { return _selectedMessage; }
            set
            {
                if (_selectedMessage == value)
                    return;

                var oldValue = _selectedMessage;
                _selectedMessage = value;
                RaisePropertyChanged("SelectedMessage", oldValue, SelectedMessage, true);
            }
        }

        private int _selectedIndex;
        public int SelectedIndex
        {
            get { return _selectedIndex; }
            set
            {
                if (_selectedIndex == value)
                    return;

                var oldValue = _selectedIndex;
                _selectedIndex = value;
                RaisePropertyChanged("SelectedIndex", oldValue, SelectedIndex, true);
            }
        }

        #endregion

        /// <summary>
        /// Initializes a new instance of the ChannelViewModel class.
        /// </summary>
        public ChannelViewModel(IMessagesView model)
        {
            if (IsInDesignMode)
            {
                // Code runs in Blend --> create design time data.
            }
            else
            {
                // Code runs "for real": Connect to service, etc...
            }
            Model = model;
            Messages = new CollectionViewSource() { Source = model.Messages };
        }

        public void Refresh(bool autoScroll)
        {
            Messages.View.Refresh();
            if (autoScroll)
                Messages.View.MoveCurrentToLast();
        }

        public override void Cleanup()
        {
            // Clean own resources if needed

            base.Cleanup();
        }

        protected override void IsBeingActivated(IStorage storage)
        {
        }

        protected override void IsBeingDeactivated(IStorage storage)
        {
        }
    }
}